package cm.cc.Velocity_Tools;



import java.io.StringWriter;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.lang.time.DateUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;


public class Velocity_Tools {
	
	  public static String templateToString(String templateXmlPath, Map<String, Object> velParamMap, String charset) throws Exception{
	        String str=null;
	        StringWriter sw =null;
	        try {
	            VelocityEngine veEngine = new VelocityEngine();
	            veEngine.init(velocityProperties);
	            Template template = veEngine.getTemplate(templateXmlPath);
	            template.process();
	            VelocityContext velocityContext = new VelocityContext();
	         //   velocityContext.put("dateUtils", DateUtils.getInstance());// 初始化一些工具类
	            Set<Map.Entry<String, Object>> set = velParamMap.entrySet();
	            for (Entry<String, Object> entry : set) {
	                velocityContext.put(entry.getKey(), entry.getValue());
	            }
	            sw = new StringWriter();
	            template.merge(velocityContext, sw);
	            str = sw.toString();
	            if (!CommonUtils.isEmpty(charset)) {
	                str = new String(str.getBytes(), charset);
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	            logger.error(ChannelCodes.ERROR,e);
	            throw new BusinessException(ChannelPinConstants.SERVICE_EXCEPTION_CODE+"@"+"请求报文节点缺失!");
	        }finally{
	            if(sw!=null){
	                sw.close();
	            }
	        }
	        str=filterXmlAnnocation(str);
	        logger.debug(str);
	        return str;
	    }
	

}
